package com.furns.service.impl;

import com.furns.dao.FurnDAO;
import com.furns.dao.impl.FurnDAOImpl;
import com.furns.entity.Furn;
import com.furns.entity.Page;
import com.furns.service.FurnService;

import java.util.List;

/**
 * @Author LuoJiaCheng
 * @ID 2240129126
 * @Version 1.0
 */
public class FurnServiceImpl implements FurnService {
    private FurnDAO furnDAO = new FurnDAOImpl();
    //返回家居信息
    @Override
    public List<Furn> queryFurns() {
        return furnDAO.queryFurns();
    }
    //判断家居是否添加成功
    @Override
    public int addFurn(Furn furn) {
        return furnDAO.addFurn(furn);
    }
    //判断家居是否删除成功
    @Override
    public int deleteFurnById(int id) {
        return furnDAO.deleteFurnById(id);
    }
    //根据id返回家居的信息
    @Override
    public Furn queryFurnById(int id) {
        return furnDAO.queryFurnById(id);
    }
    //删除家居信息
    @Override
    public int updateFurn(Furn furn) {
        return furnDAO.updateFurn(furn);
    }

    @Override
    public Page<Furn> page(int pageNo, int pageSize) {
        //先创建一个Page对象，根据实际填充属性
        Page<Furn> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow = furnDAO.getTotalRow();
        page.setTotalRow(totalRow);
        int pageTotalCount = totalRow/pageSize;
        if(totalRow % pageSize>0){
            pageTotalCount+=1;
        }
        page.setPageTotalCount(pageTotalCount);

        int begin = (pageNo-1)*pageSize;
        List<Furn> pageItems = furnDAO.getPageItems(begin, pageSize);

        page.setItems(pageItems);

        //分页导航
        return page;
    }

    @Override
    public Page<Furn> pageByName(String name, int pageNo, int pageSize) {
        //先创建一个Page对象，根据实际填充属性
        Page<Furn> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow = furnDAO.getPageTotalCountByName(name);
        page.setTotalRow(totalRow);
        int pageTotalCount = totalRow/pageSize;
        if(totalRow % pageSize>0){
            pageTotalCount+=1;
        }
        page.setPageTotalCount(pageTotalCount);

        int begin = (pageNo-1)*pageSize;
        List<Furn> pageItemsByName = furnDAO.getPageItemsByName(name, begin, pageSize);
        page.setItems(pageItemsByName);
        //url
        return page;
    }
}
